package com.xj.center.manager.api;

import com.alibaba.fastjson.JSONObject;
import com.ie.api.center.manager.feign.DriverClient;
import com.xj.center.manager.service.DriverService;
import com.xj.center.manager.service.TenantService;
import com.xj.common.bean.R;
import com.xj.common.bean.constant.Common;
import com.xj.common.bean.driver.DriverConfiguration;
import com.xj.common.model.Driver;
import com.xj.common.model.Tenant;
import com.xj.common.utils.JsonPaseUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * driver数据接口
 * @author wsp
 *
 */
@RestController
@RequestMapping(value = Common.Service.IE_MANAGER_DRIVER)
public class DriverAPI implements DriverClient {

	@Resource
	private DriverService driverService;

	@RequestMapping(value = Common.Service.IE_MANAGER_DRIVER_ADD,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	@Override
	public R<Driver> add(
			@RequestBody JSONObject jsonParam) {
		try {
			Driver driver = JsonPaseUtil.JSONStrToObj(jsonParam.toJSONString(), Driver.class);
			Driver add = driverService.add(driver);
			if (null != add) {
				return R.ok(add);
			}
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
		return R.fail();
	}

	@RequestMapping(value = Common.Service.IE_MANAGER_DRIVER_DELETE,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	@Override
	public R<Driver> delete(
			@RequestBody JSONObject jsonParam) {
		try {
			return driverService.delete(jsonParam.getLong("id")) ? R.ok() : R.fail();
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}
	@RequestMapping(value = Common.Service.IE_MANAGER_DRIVER_UPDATE,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	@Override
	public R<Driver> update(
			@RequestBody JSONObject jsonParam) {
		try {
			 Driver driver = JsonPaseUtil.JSONStrToObj(jsonParam.toJSONString(), Driver.class);
			Driver update = driverService.update(driver);
			if (null != update) {
				return R.ok(update);
			}
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
		return R.fail();
	}

	@RequestMapping(value = Common.Service.IE_MANAGER_DRIVER_SELECT_BY_ID,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	@Override
	public R<Driver> selectById(
			@RequestBody JSONObject jsonParam){
		try {
			Driver select = driverService.selectById(jsonParam.getLong("id"));
			return R.ok(select);
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}

	@RequestMapping(value = Common.Service.IE_MANAGER_DRIVER_SELECT_BY_TENANTTID,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	@Override
	public R<List<Driver>> selectByTenantId(
			@RequestBody JSONObject jsonParam) {
		try {
			List<Driver> select = driverService.selectByTenantId(jsonParam.getLong("id"));
			return R.ok(select);
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}

	@RequestMapping(value = Common.Service.IE_MANAGER_DRIVER_SELECT_BY_SERVICENAME,method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@CrossOrigin
	@Override
	public R<Driver> selectByServiceName(
			@RequestBody JSONObject jsonParam) {
		try {
			Driver select = driverService.selectByServiceName(jsonParam.getString("serviceName"));
			return R.ok(select);
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}

}
